package com.zlj.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zlj.springcloud.entity.Dept;
import com.zlj.springcloud.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: zlj
 * @CreateDate: 2018/6/13 15:25
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeptController.class);

    private final DeptService deptService;

    private DiscoveryClient client;

    public DeptController(DeptService deptService, DiscoveryClient client) {
        this.deptService = deptService;
        this.client = client;
    }

    @GetMapping("/get/{deptNo}")
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Dept get(@PathVariable("deptNo") Long deptNo) {
        Dept dept = deptService.get(deptNo);
        if (dept == null) {
            throw new RuntimeException("该ID: " + deptNo + "没有对应的信息");
        }
        return dept;
    }

    public Dept processHystrix_Get(@PathVariable("deptNo") Long deptNo) {
        return new Dept().setDeptno(deptNo).setDb_source("no this database in MySQL").
                setDname("该ID: " + deptNo + "没有对应信息 - null -Hystrix");
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return deptService.list();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return deptService.add(dept);
    }
}


